package provider;

import rpc.constant.RpcConstant;
import service.UserService;
import rpc.RpcApplication;
import rpc.config.RpcConfig;
import rpc.config.RegistryConfig;
import rpc.message.ServiceMetaInfo;
import rpc.registry.Registry;
import rpc.registry.RegistryFactory;
import rpc.server.HttpServer;
import rpc.server.VertxHttpServer;
import rpc.registry.LocalRegistry;



public class provider {
    public static void main(String[] args) {
        RpcApplication.init();
        //注册服务
        String serviceName=UserService.class.getName();
        LocalRegistry.register(serviceName,UserServiceImpl.class);
        //注册到服务中心
        RpcConfig rpcConfig=RpcApplication.getRpcConfig();
        RegistryConfig registryConfig=rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance();
        ServiceMetaInfo serviceMetaInfo=new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        serviceMetaInfo.setServiceVersion(RpcConstant.DEFAULT_SERVICE_VERSION);
        try {
            registry.register(serviceMetaInfo);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        // 启动 web 服务
        HttpServer httpServer =new VertxHttpServer();
        httpServer.doStart(RpcApplication.getRpcConfig().getServerPort());
    }
}
